package com.slj.controller;

import com.slj.comm.responce.R;
import com.slj.model.entity.Categories;
import com.slj.service.CategoriesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
@Tag(name = "类别管理")
public class CategoriesController {

    @Autowired
    private CategoriesService categoriesService;

    // 获取所有类别
    @PostMapping("/getCategoryList")
    @Operation(summary = "获取类别列表")
    public R<List<Categories>> getCategoryList() {
        List<Categories> list = categoriesService.list();
        return R.success(list);
    }

    // 添加类别
    @PostMapping("/add")
    @Operation(summary = "添加类别")
    public R<String> addCategory(@RequestBody Categories category) {
        boolean saved = categoriesService.save(category);
        return saved ? R.success("添加成功") : R.fail("添加失败");
    }

    // 更新类别
    @PostMapping("/update")
    @Operation(summary = "更新类别")
    public R<String> updateCategory(@RequestBody Categories category) {
        boolean updated = categoriesService.updateById(category);
        return updated ? R.success("更新成功") : R.fail("更新失败");
    }

    // 删除类别
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除类别")
    public R<String> deleteCategory(@PathVariable Integer id) {
        boolean removed = categoriesService.removeById(id);
        return removed ? R.success("删除成功") : R.fail("删除失败");
    }

    // 获取类别详情
    @GetMapping("/detail/{id}")
    @Operation(summary = "获取类别详情")
    public R<Categories> getCategoryDetail(@PathVariable Integer id) {
        Categories category = categoriesService.getById(id);
        return category != null ? R.success(category) : R.fail("类别不存在");
    }
}